import Vue from 'vue'
const filterDictionary = {
  tofixed(value, precision) {
    if (!value || typeof value != 'number') return value
    value = value.toFixed(precision || 2);
    return value
  },
  tofixedInt(value) {
    if (!value || typeof value != 'number') return value
    value = Math.round(value);
    return value
  },
  commaFormat(value, precision){
    if (!value || typeof value != 'number') return value
    value = value.formatNumber(precision);
    return value
  },
  cutting(value, unit){
    if (typeof value !== 'number')
      return 0;
    const scale = 1024;
    switch (unit){
      case 'g':
        value = value / (scale * scale * scale);
        unit = 'GB';
        break;
      case 'm':
        value = value / (scale * scale);
        unit = 'MB';
        break;
      case 'k':
        value = value / scale;
        unit = 'KB';
        break;
      case 'b':
        unit = 'B';
        break;
      default:
        if (value > (scale * scale)){
          value = value / (scale * scale);
          unit = 'MB';
        }
        else if (value > scale){
          value = value / scale;
          unit = 'KB';
        }
        else{

          unit = 'B';
        }
        break;
    }
    return value.formatNumber() + unit;
  },
  datetimeFormat(value, pattern) {

    if (!value) {
      return value
    }
    if (typeof value == 'string') {
      value = value.toDate();
    }

    if (value instanceof Date) {
      pattern = pattern || 'yyyy-MM-dd HH:mm:ss';

      value = value.format(pattern);
      return value
    } else {
      return value
    }
  },
  bankNo(value) {
    if (value && typeof value == 'string') {
      return value.substr(0, 4) + "********" + value.substr(-4);
    } else {
      return value;
    }
  },
  cellphone(value) {
    if (value && typeof value == 'string') {
      return value.substr(0, 3) + "****" + value.substr(-4);
    } else {
      return value;
    }
  },
  timeAgo(time) {
    const between = Date.now() / 1000 - Number(time)
    if (between < 3600) {
      return pluralize(~~(between / 60), ' minute')
    } else if (between < 86400) {
      return pluralize(~~(between / 3600), ' hour')
    } else {
      return pluralize(~~(between / 86400), ' day')
    }
  }
}

Object.keys(filterDictionary).forEach(key => {
  Vue.filter(key, filterDictionary[key])
})
